package com.starry.module.system.core.file.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.starry.core.common.constants.CommonError;
import com.starry.core.common.domain.vo.PageVo;
import com.starry.core.common.execption.ServiceException;
import com.starry.db.core.query.LambdaQueryWrapperX;
import com.starry.module.system.api.file.domain.qo.FileConfigPageQo;
import com.starry.module.system.api.file.domain.qo.FileConfigQo;
import com.starry.module.system.api.file.domain.vo.FileConfigDtlVo;
import com.starry.module.system.api.file.domain.vo.FileConfigVo;
import com.starry.module.system.core.file.client.FileClient;
import com.starry.module.system.core.file.entity.FileConfig;
import com.starry.module.system.core.file.mapper.FileConfigMapper;
import com.starry.module.system.core.file.service.FileClientFactory;
import com.starry.module.system.core.file.service.FileConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * 文件配置-服务实现
 * @author xiaoke
 */
@Service("fileConfigService")
public class FileConfigServiceImpl implements FileConfigService {

    private final FileConfigMapper fileConfigMapper;
    private final FileClientFactory fileClientFactory;

    public FileConfigServiceImpl(FileConfigMapper fileConfigMapper, FileClientFactory fileClientFactory) {
        this.fileConfigMapper = fileConfigMapper;
        this.fileClientFactory = fileClientFactory;
    }

    @Override
    public PageVo<FileConfigVo> page(FileConfigPageQo pageQo) {
        PageVo<FileConfig> page = fileConfigMapper.selectPage(pageQo);
        List<FileConfigVo> vos = BeanUtil.copyToList(page.getRows(), FileConfigVo.class);
        return new PageVo<>(vos, page.getTotal());
    }

    @Override
    public FileConfigDtlVo getById(Long id) {
        FileConfig fileConfig = fileConfigMapper.selectById(id);
        if (fileConfig == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        return BeanUtil.toBean(fileConfig, FileConfigDtlVo.class);
    }

    @Override
    public FileClient getMasterClient() {
        FileConfig fileConfig = fileConfigMapper.selectByMaster();
        FileClient fileClient = fileClientFactory.getFileClient(fileConfig.getId());
        if (fileClient != null) {
            return fileClient;
        }
        return fileClientFactory.createOrUpdateFileClient(fileConfig);
    }

    @Override
    public FileClient getFileClient(Long configId) {
        FileClient fileClient = fileClientFactory.getFileClient(configId);
        if (fileClient != null) {
            return fileClient;
        }
        FileConfig fileConfig = fileConfigMapper.selectById(configId);
        if (fileConfig == null) {
            throw new ServiceException("文件配置不存在");
        }
        return fileClientFactory.createOrUpdateFileClient(fileConfig);
    }

    @Transactional
    @Override
    public Long save(FileConfigQo fileConfigQo) {
        LambdaQueryWrapperX<FileConfig> wrapper = new LambdaQueryWrapperX<>();
        wrapper
                .eq(FileConfig::getName, fileConfigQo.getName());
        if (fileConfigMapper.selectCount(wrapper) > 0) {
            throw new ServiceException(CommonError.DATA_EXISTS.getMsg());
        }
        FileConfig fileConfig = BeanUtil.toBean(fileConfigQo, FileConfig.class);
        fileConfig.setId(null);
        fileConfigMapper.insert(fileConfig);
        return fileConfig.getId();
    }

    @Transactional
    @Override
    public void updateById(FileConfigQo fileConfigQo) {
        if (fileConfigQo.getId() == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        LambdaQueryWrapperX<FileConfig> wrapper = new LambdaQueryWrapperX<>();
        wrapper
                .ne(FileConfig::getId, fileConfigQo.getId())
                .and(w ->
                        w.eq(FileConfig::getName, fileConfigQo.getName())
                );
        if (fileConfigMapper.selectCount(wrapper) > 0) {
            throw new ServiceException(CommonError.DATA_EXISTS.getMsg());
        }
        FileConfig fileConfig = fileConfigMapper.selectById(fileConfigQo.getId());
        if (fileConfig == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        BeanUtil.copyProperties(fileConfigQo, fileConfig);
        fileConfigMapper.updateById(fileConfig);

        // 删除文件配置缓存
        fileClientFactory.deleteFileClient(fileConfig.getId());
    }

    @Transactional
    @Override
    public void removeByIds(Set<Long> idList) {
        if (idList.isEmpty()) return;
        fileConfigMapper.deleteByIds(idList);
    }

    @Transactional
    @Override
    public void setByIdConfigMaster(Long id) {
        FileConfig fileConfig = fileConfigMapper.selectById(id);
        if (fileConfig == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        // 设置所有为非主配置
        fileConfigMapper.setConfigNotAllMaster();

        // 设置当前为主配置
        fileConfig.setMaster(1);
        fileConfigMapper.updateById(fileConfig);
    }
}

